package com.example.design01.controller;

import com.example.design01.Bean.StaffBean;
import com.example.design01.Bean.UserBean;
import com.example.design01.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    //    登陆
    @RequestMapping("/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpSession session) {
        UserBean userBean = userService.checkUser(username, password);
        if (userBean != null) {
            session.setAttribute("LoginUser", username);
            return "redirect:/main.html";
        } else {
            model.addAttribute("msg", "输入有误请重新输入");
            return "index";
        }
    }
    //    退出登录
    @RequestMapping("/loginOut")
    public String logout(HttpSession session) {
        session.removeAttribute("LoginUser");
        return "index";
    }
    //    注册跳转
    @RequestMapping("/toAddUser")
    public String toAddUser() {
        return "addUser";
    }

    //    注册
    @RequestMapping("/addUser")
    public String addUSer(UserBean userBean) {
        userService.addUser(userBean);
        return "index";
    }


    //    修改跳转
    @RequestMapping("/toUpdateUser")
    public String toUpdate() {
        return "updateUser";
    }

    //修改
    @RequestMapping("/updateU")
    public String updateStaff(@RequestParam("password") String password) {
        userService.updateUser(password);
        return "index";
    }
}
